package com.jdd.pedicure.service;

import com.jdd.pedicure.dao.AdminDao;
import com.jdd.pedicure.entity.Admin;
import com.jdd.pedicure.entity.Result;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class AdminService {
    @Resource
    AdminDao adminDao;

    public Admin queryById(int id) {
        return adminDao.getOne(id);
    }

    public List<Admin> queryAll() {
        return adminDao.findAll();
    }

    public void delete(int id) {
        adminDao.deleteById(id);
    }

    public void deletes(List<Admin> admins){adminDao.deleteAll(admins);}
    public void saveOrUpdate(Admin admin) {
        adminDao.save(admin);
    }

    public Result<Admin> queryByPage(Result<Admin> result) {
        PageRequest request = new PageRequest(result.getPage() - 1, result.getLimit());
        Example<Admin> ex = Example.of(result.getExample());
        Page<Admin> all = adminDao.findAll(ex, request);
        result.setData(all.getContent());
        result.setCount((int) all.getTotalElements());
        return result;
    }

    public Admin login(Admin admin)
    {
        Admin user = adminDao.userlogin(admin.getUserName(),admin.getPassword());
        return user;
    }
}
